﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Gao.Poker.Domain;
using Gao.Poker.Services;
using Gao.Poker.ViewModel;

namespace Gao.Poker.App.Win
{
    /// <summary>
    /// Interaction logic for SendSmsWin.xaml
    /// </summary>
    public partial class SendSmsWin : Window
    {
        public SendSmsViewModel ViewModel { get; set; }

        public SendSmsWin()
        {
            InitializeComponent();

            ViewModel = new SendSmsViewModel();
            ViewModel.Message = OnMessage;
            DataContext = ViewModel;
        }

        private void OnMessage(string obj)
        {
            MessageBox.Show(obj, "提示消息");
        }

        private void MoveLeft_Click(object sender, RoutedEventArgs e)
        {
            List<Member> list = new List<Member>();
            foreach (var item in dgRight.SelectedItems)
            {
                var member = item as Member;
                if (member != null)
                {
                    list.Add(member);
                }
            }

            foreach (var member in list)
            {
                ViewModel.SendMembers.Remove(member);
            }

            ViewModel.OnPropertyChanged("SendMembers");
            ViewModel.OnPropertyChanged("Members");
        }

        private void MoveRight_Click(object sender, RoutedEventArgs e)
        {
            List<Member> list = new List<Member>();
            foreach (var item in dgLeft.SelectedItems)
            {
                var member = item as Member;
                if(member != null)
                {
                    ViewModel.SendMembers.Add(member);
                }
            }
            //dgRight.ItemsSource = list;
            ViewModel.OnPropertyChanged("SendMembers");
            ViewModel.OnPropertyChanged("Members");
        }

        private void SendMsm_Click(object sender, RoutedEventArgs e)
        {
            SendSmsService service = new SendSmsService();
            ViewModel.SendSms.SendSmsMembers.Clear();
            foreach (var member in ViewModel.SendMembers)
            {
                var sm = new SendSmsMember();
                sm.MemberId = member.ID;
                sm.SendTime = DateTime.Now;
                sm.Name = member.Name;
                sm.Telephone = member.Telephone;

                ViewModel.SendSms.SendSmsMembers.Add(sm);
            }
            service.SendSms(ViewModel.SendSms);

            OnMessage("发送成功");
            Close();
        }

        private void SearchMember_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.OnPropertyChanged("Members");
        }
    }
}
